define(['jquery'], function($) {
    'use strict';

    var getStateParams = (function() {
        var sSeach = location.search.split('?')[1];
        var json = {};
        if (sSeach) {
            var arr = sSeach.split('&');
            for (var i = 0; i < arr.length; i++) {
                var aTmp = arr[i].split('=');
                json[aTmp[0]] = aTmp[1];
            }
        }
        return json;
    })();

    function isObject(value) {
        return value !== null && typeof value === 'object';
    }

    $(function() {
        /*关键词*/
        var param = {
            id: getStateParams.id,
            '__CSRF__': APP.G_csrf
        };

        $('.list-left .text').text(decodeURIComponent(getStateParams.cname) + '热门论文');
        $.get("/list/index/keywordlist", param).then(function(response) {
            if (response.code == 200) {
                var data = response.message;
                var papers = data.papers;
                if (papers.length) {
                    /*渲染左侧列表*/
                    var aTr = [];
                    var aTrs = [];
                    var aTds = [];
                    papers.forEach(function(n, index) {
                        var sTd = "<td><a href=" + n.url + " target='_blank'>" + n.title + "</a></td>";
                        aTds.push(sTd);
                    });
                    for (var j = 0; j < (aTds.length / 2); j++) {
                        (function(index) {
                            var sTd = aTds.slice((index * 2), (index * 2 + 2));
                            aTr.push(sTd);
                        })(j);
                    }
                    aTr.forEach(function(n, index) {
                        var sTr = "<tr>" + n.join('') + "</tr>";
                        aTrs.push(sTr);
                    });
                    var sLeftTabel = ["<div class=\"scroll\">",
                        "<table class=\"m-table\">",
                        "  <tbody>" + aTrs.join('') + "</tbody>",
                        "  </table>",
                        "</div>"
                    ].join("");
                    $('.list-left').append(sLeftTabel);
                } else {
                    var sTr = ["<tr>",
                        "       <td colspan='3'>暂时没有数据</td>",
                        "</tr>"
                    ];
                    var sLeftTabel = ["<table class=\"m-table\">",
                        "  <thead>",
                        "   <tr>",
                        "      <th class=\"cola\">名称</th>",
                        "      <th>综合评分</th>",
                        "      <th class=\"colb\">高校 TOP10</th>",
                        "   </tr>",
                        "  </thead>",
                        "  <tbody>" + sTr.join('') + "</tbody>",
                        "</table>"
                    ].join("");
                    $('.list-left').append(sLeftTabel);
                }
                return data;
            }
        });
    });
});
